﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteLotteriesService : ILotteriesService
    {  
       IUnitOfWork unitOfWork;
       IRepository<VT_Lotteries> lotteriesRepository;
       IRepository<VT_Tickets> ticketsRepository;

       public VoteLotteriesService()
       {
           unitOfWork = Factory.GetInstance<IUnitOfWork>();
           lotteriesRepository = unitOfWork.GetRepository<VT_Lotteries>();
           ticketsRepository = unitOfWork.GetRepository<VT_Tickets>();
       }
  
       public bool AddWeekLotteries(string activityID, string ticketID, string userEmail)
       {
           VT_Lotteries v_Lotteries = new VT_Lotteries();
           unitOfWork.BeginTransaction();
           v_Lotteries.Lottery_ID = Guid.NewGuid().ToString();
           v_Lotteries.Lottery_Level = 0;
           v_Lotteries.Lottery_Type = 0;
           v_Lotteries.Ticket_ID = ticketID;
           v_Lotteries.Created_Date = DateTime.Now;
           v_Lotteries.User_Email = userEmail;
           v_Lotteries.Activity_ID = activityID;
           lotteriesRepository.Add(v_Lotteries);
           unitOfWork.CommitTransaction();
           return true;
       }
       public bool AddActivityLotteries(string activityID, string ticketID, string userEmail,int level)
       {
           VT_Lotteries v_Lotteries = new VT_Lotteries();
           int type = 1;
           unitOfWork.BeginTransaction();
           v_Lotteries.Lottery_ID = Guid.NewGuid().ToString();
           v_Lotteries.Lottery_Level = level;
           v_Lotteries.Lottery_TypeValue = type; 
           v_Lotteries.Ticket_ID = ticketID;
           v_Lotteries.Created_Date = DateTime.Now;
           v_Lotteries.User_Email = userEmail;
           v_Lotteries.Activity_ID = activityID;
           lotteriesRepository.Add(v_Lotteries);
           unitOfWork.CommitTransaction();
           return true;
       }
    
       public bool DeleteLotteries(string LotteriesID)
       {
           VT_Lotteries v_Lotteries = lotteriesRepository.GetMany(o => o.Lottery_ID == LotteriesID).FirstOrDefault();
           if (v_Lotteries != null)
           {
               unitOfWork.BeginTransaction();
               lotteriesRepository.Delete(v_Lotteries);
               unitOfWork.CommitTransaction();
               return true;
           }
           return false;
       }

       public int GetActivityLotteriesCount(string activityID)
       {
           int i = lotteriesRepository.GetMany(o => o.Activity_ID == activityID && o.Lottery_TypeValue == 1).Count();
           return i;
       }
       //根据活动ID查询     
       public IList<VT_Lotteries> GetWeekLotteriesList(string activityID)
       {
           IList<VT_Lotteries> lotteries = lotteriesRepository.GetMany(o => o.Activity_ID == activityID && o.Lottery_TypeValue == 0).OrderByDescending(o=>o.Created_Date).ToList();
           return lotteries;
       }

       //根据活动ID查询     
       public IList<VT_Lotteries> GetActivityLotterysList(string activityID)
       {
           IList<VT_Lotteries> lotteries = lotteriesRepository.GetMany(o => o.Activity_ID == activityID && o.Lottery_TypeValue == 1).OrderByDescending(o => o.Created_Date).OrderBy(o=>o.Lottery_Level).ToList();
           return lotteries;
       }
       #region IDisposable Members

        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();   
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteLotteriesService()
        {
            Dispose(false);
        }
        #endregion
    }

}
